<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
if($_RQ['op']=='get_detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('没有任务ID','error','jy_renwushop/index/');
	}
	$renwu=pdo_get('jy_renwushop_renwu',array('id'=>$id,'status'=>1,'uniacid'=>$_SESSION['uniacid']));
	if(!$renwu){
		exi('选择的任务不存在或已下架，请重新选择！','error','/index/');
	}
	pdo_update('jy_renwushop_renwu',array('looks +='=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$_SESSION['renwuid'] = $renwu['id'];
	$return['renwuurl']=custom_url('jy_renwushop/renwu.detail//id='.$id,true);
	$return['sharedata']=array(
		'title'=>$renwu['title'],
		'desc'=>$renwu['desc']?$renwu['desc']:$renwu['title'],
		'link'=>custom_url('jy_renwushop/renwu.detail//id='.$id.'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
		'imgUrl'=>tomedia($renwu['image'])
	);
	$renwu['isnum']=pdo_count('jy_renwushop_renwuorder',array('uniacid'=>$_SESSION['uniacid'],'renwuid'=>$renwu['id']),'id');
	$renwu['lastnum']=$renwu['totalnum']-$renwu['isnum'];
	$renwu['typetitle']=$this->renwu_types[$renwu['type']];
	$renwu['hours_typetitle']=$this->hours_type[$renwu['hours_type']];
	$return['fauser']=cfc('core')->member_show($renwu['mid']);
	$myorder=pdo_get('jy_renwushop_renwuorder',array('mid'=>$_SESSION['uid'],'status'=>0,'renwuid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($myorder['status']==0){
		$myorder['statustitle']='您已报名';
	}elseif($myorder['status']==1){
		$myorder['statustitle']='您已提交';
	}elseif($myorder['status']==2){
		$myorder['statustitle']='已完成';
	}elseif($myorder['status']==3){
		$myorder['statustitle']='不合格';
	}elseif($myorder['status']==4){
		$myorder['statustitle']='已过期';
	}
	$return['subdata']=array('id'=>$myorder['id']);
	$return['post']=array('id'=>$myorder['id']);
	$return['myorder']=$myorder;
	$renwu['steps']=json_decode($renwu['steps'],true);
	$return['renwu']=$renwu;
	$this->model('task')->orderupone($renwu);
	set_html('title',$renwu['title']?:'任务详情');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	$renwu=pdo_get('jy_renwushop_renwu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$isnum=pdo_count('jy_renwushop_renwuorder',array('uniacid'=>$_SESSION['uniacid'],'renwuid'=>$renwu['id']),'id');
	if($isnum>=$renwu['totalnum']){
		exi('任务已接完，下次再来吧！','error');
	}
	if($renwu['isstop']){
		exi('任务已关闭！','error');
	}
	if($renwu['times_type']==1){
		$ismynum=pdo_count('jy_renwushop_renwuorder',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'renwuid'=>$renwu['id']),'id');
		if($ismynum){
			exi('该任务每人只能做一次，去接其他任务吧！','error');
		}
	}elseif($renwu['times_type']==2){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$ismynum=pdo_count('jy_renwushop_renwuorder',array('createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'renwuid'=>$renwu['id']),'id');
		if($ismynum){
			exi('该任务每天只能做一次，去接其他任务吧！','error');
		}
	}
	$order=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'type'=>$renwu['type'],
		'renwuid'=>$renwu['id'],
		'fee'=>$renwu['fee'],
		'num'=>1,
		'ordersn'=>get_ordersn($this->tablename('renwuorder'),'ordersn'),
		'status'=>0,
		'createtime'=>time(),
	);
	$this->pdo_insert('renwuorder',$order);
	exi('报名成功，赶紧完成任务吧！');
}
if($_RQ['op']=='subdata'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$post['title']){
		exi('请填写任务提交说明！','error');
	}
	$isstop=pdo_getcolumn('jy_renwushop_renwu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'isstop');
	if($isstop){
		exi('该任务已关闭！','error');
	}
	$order=array(
		'subdata'=>ijson_encode($post),
		'status'=>1,
		'subtime'=>time(),
	);
	$this->pdo_update('renwuorder',$order,array('id'=>$id));
	exi('任务提交完成！','','/renwuorder/');
}
if($_RQ['op']=='check_buy'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	model('jy_weishop/goods')->check_buy($goodsid,$_RQ['num'],$_RQ['optionsid']);
	if($_RQ['single']){
		exi('','','jy_weishop/order.create//goodsid='.$goodsid.'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid'].'&roomid='.$_RQ['roomid']);
	}else{
		$this->model('model')->checkteam($goodsid);
		exi('','','jy_renwushop/order.create//teamid='.$_RQ['teamid'].'&goodsid='.$goodsid.'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid'].'&roomid='.$_RQ['roomid']);
	}
}